package config

import (
	"github.com/lishimeng/app-starter"
	"github.com/lishimeng/app-starter/server"
	"github.com/lishimeng/go-log"
	"net/http"
)

type Xiaci struct {
	Width  int `json:"width,omitempty"`
	Height int `json:"height,omitempty"`
	Size   int `json:"size,omitempty"`
}

type Houdu struct {
	Width     int     `json:"width,omitempty"`
	Height    int     `json:"height,omitempty"`
	Threshold float64 `json:"threshold,omitempty"`
	Step      float64 `json:"step,omitempty"`
	Filter    int     `json:"filter,omitempty"`
}

type Chart3DConfig struct {
	Houdu Houdu `json:"houdu,omitempty"`
	Xiaci Xiaci `json:"xiaci,omitempty"`
}

var defaultConfig = Chart3DConfig{}

func init() {
	defaultConfig.Xiaci.Size = 10
	defaultConfig.Xiaci.Width = 30
	defaultConfig.Xiaci.Height = 50

	defaultConfig.Houdu.Height = 1000
	defaultConfig.Houdu.Width = 90
	defaultConfig.Houdu.Threshold = 80.0
	defaultConfig.Houdu.Step = 0.5
	defaultConfig.Houdu.Filter = 8
}

func config3D(ctx server.Context) {

	var req Chart3DConfig
	var err error
	var resp app.Response

	err = ctx.C.ReadJSON(&req)
	if err != nil {
		log.Info(err)
		resp.Code = http.StatusBadRequest
		resp.Message = err.Error()
		ctx.Json(resp)
		return
	}
	if req.Houdu.Width > 0 {
		defaultConfig.Houdu.Width = req.Houdu.Width
	}
	if req.Houdu.Height > 0 {
		defaultConfig.Houdu.Height = req.Houdu.Height
	}
	if req.Xiaci.Size > 0 {
		defaultConfig.Xiaci.Size = req.Xiaci.Size
	}
	if req.Xiaci.Width > 0 {
		defaultConfig.Xiaci.Width = req.Xiaci.Width
	}
	if req.Xiaci.Height > 0 {
		defaultConfig.Xiaci.Height = req.Xiaci.Height
	}

	resp.Code = http.StatusOK
	ctx.Json(resp)
}

func get3DConfig(ctx server.Context) {
	var resp app.ResponseWrapper
	resp.Code = http.StatusOK
	resp.Data = defaultConfig
	ctx.Json(resp)
}
